package club.emergency.wechat.ranking.search.provider.impl;

import club.emergency.wechat.common.response.ResponseEntity;
import club.emergency.wechat.common.response.StatusCode;
import club.emergency.wechat.common.utils.JsonConvert;
import club.emergency.wechat.model.ranking.Ranking;
import club.emergency.wechat.model.staff.Staff;
import club.emergency.wechat.persistence.staff.StaffMapper;
import club.emergency.wechat.ranking.search.RankingSearch;
import club.emergency.wechat.redis.common.RedisKeyConstant;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * Create with IntelliJ IDEA
 * Project Name: wechat
 * Package Name: club.emergency.wechat.ranking.search.provider.impl
 * Date: 2019/2/25
 *
 * @author: 快乐的小菌菌
 * Description:
 */
@RestController
public class RankingSearchProviderImpl implements RankingSearch {

    private StringRedisTemplate stringRedisTemplate;

    private StaffMapper staffMapper;

    public RankingSearchProviderImpl(StringRedisTemplate stringRedisTemplate, StaffMapper staffMapper) {
        this.stringRedisTemplate = stringRedisTemplate;
        this.staffMapper = staffMapper;
    }

    @Override
    public String rankingList(@RequestParam("endIndex") Integer endIndex) {
        ZSetOperations<String, String> stringStringZSetOperations = stringRedisTemplate.opsForZSet();
        if (Objects.requireNonNull(stringRedisTemplate.hasKey(RedisKeyConstant.RANKING_CACHE))) {
            ValueOperations<String, String> valueOperations = stringRedisTemplate.opsForValue();
            String rankingList = valueOperations.get(RedisKeyConstant.RANKING_CACHE);
            return ResponseEntity.ok(StatusCode.REDIS_GET_OK, "查询排行榜信息成功！", new JsonConvert<Ranking>().jsonListDecode(rankingList, Ranking.class));
        }
        if (!Objects.requireNonNull(stringRedisTemplate.hasKey(RedisKeyConstant.RANKING))) {
            return null;
        }
        Set<ZSetOperations.TypedTuple<String>> typedTuples = stringStringZSetOperations.rangeWithScores(RedisKeyConstant.RANKING, 0L, endIndex.longValue());
        List<Ranking> rankingList = new ArrayList<>();
        Objects.requireNonNull(typedTuples).forEach(typedTuple -> {
            Ranking ranking = new Ranking();
            ranking.setStaffId(Long.valueOf(Objects.requireNonNull(typedTuple.getValue())));
            ranking.setScore(Objects.requireNonNull(typedTuple.getScore()).intValue());
            Staff staff = staffMapper.selectById(typedTuple.getValue());
            ranking.setOpenid(staff.getOpenId());
            ranking.setHeadImgUrl(staff.getHeadimgurl());
            ranking.setNickName(staff.getNickname());
            rankingList.add(ranking);
        });
        Collections.reverse(rankingList);
        ValueOperations<String, String> valueOperations = stringRedisTemplate.opsForValue();
        valueOperations.set(RedisKeyConstant.RANKING_CACHE, new JsonConvert<List>().jsonEncode(rankingList));
        return ResponseEntity.ok(StatusCode.REDIS_GET_OK, "查询排行榜信息成功！", rankingList);
    }

}
